package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.api.entity.SysRoleEntity;
import com.qf.api.entity.SysRoleMenuEntity;
import com.qf.api.service.SysRoleMenuService;
import com.qf.api.service.SysRoleService;
import com.qf.api.vo.OptionsVO;
import com.qf.api.vo.RolePageVO;
import com.qf.dao.mapper.SysRoleMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: sin
 * @Date: 2025/5/8 18:18
 * @Description:
 **/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity>
        implements SysRoleService {

    @Resource
    private SysRoleMapper roleMapper;

    @Resource
    private SysRoleMenuService roleMenuService;


    @Override
    public List<OptionsVO> getRoleOption() {
        return roleMapper.getRoleOption();
    }

    @Override
    public List<RolePageVO> getRolePage(String name, Integer status, String code) {
        return roleMapper.getRolePage(name,status,code);
    }

    @Override
    public List<Long> getRoleMenuByRoleId(Long roleId) {
        return roleMapper.getMenuByRoleId(roleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRoleMenu(Long roleId, List<Long> menuIds) {
        roleMenuService.remove(new QueryWrapper<SysRoleMenuEntity>().eq("role_id",roleId));
        List<SysRoleMenuEntity> roleMenus = menuIds.stream().map(menuId -> new SysRoleMenuEntity(roleId, menuId)).collect(Collectors.toList());
        return roleMenuService.saveBatch(roleMenus);
    }

    @Override
    public boolean updateRoleById(Long id, String name, String code, String remark) {
        return roleMapper.updateRoleById(id,name,code,remark) > 0;
    }
}
